package com.example.market2023.service;
import com.example.market2023.dao.UserDao;
import com.example.market2023.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService implements UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql = "insert into sys_user values(null,'"+user.getName()+"','"+user.getPassword()+"','0','0')";
        System.out.println(sql);
        return jdbcTemplate.update(sql);
    }

    @Override
    public int delete(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public User findOne(User user) {
        return jdbcTemplate.queryForObject("select * from sys_user where name like '%"+user.getName()+"%'",new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public List<User> findAll() {
        return jdbcTemplate.query("select * from sys_user",new BeanPropertyRowMapper<User>(User.class));
    }
}

